package com.alcatel.axs.resources;


import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alcatel.axs.model.Server;
import com.alcatel.axs.model.ServerStatus;
import com.alcatel.axs.service.ClusterManagerService;




@Component
@Path("/")
public class ClusterManagerResource {
    
    private static final Logger m_logger = Logger.getLogger(ClusterManagerResource.class);

    @Autowired
    ClusterManagerService m_service;
    
     @Autowired
    public ClusterManagerResource(ClusterManagerService clusterManagerServiceImpl) {
        m_service = clusterManagerServiceImpl;
    }
  /*
    public void setService(ClusterManagerServiceImpl clusterManagerServiceImpl) {
        service = clusterManagerServiceImpl;
    }*/

    @GET
    @Path("/{ip}/status")
    @Produces(MediaType.TEXT_HTML)
    public String getServerStatusHTML(@PathParam("ip") String ip) {
        String result="";
        try {
            for (String s:m_service.getStatus(ip)){
                result+="<pre>"+s+"<br>"+"</pre>";
            }
        } catch (Exception e) {
            result="<font color=\"#DC143C\">Server "+ip+" is not ams server!</font>";
            m_logger.error(e.getMessage(),e);
            
        }
        return result;
    }
    
    @GET
    @Path("/json/getServerList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getServerListJSON() {
        String result="";
        try {
            List<Server> list=m_service.getServer();
            result+="[{label: 'Server', id: 1,  children: [";
            for (int i=0;i<list.size();i++){
                if (i<(list.size()-1)) {
                    //result+="{\"ip\":\""+ (list.get(i).getIp() +"\"},");
                    result+="{label: '"+ (list.get(i).getIp() +"'}, ");
                }else {
                    result+="{label: '"+ (list.get(i).getIp() +"'}]}]");
                }
            }
        } catch (Exception e) {
            m_logger.error(e.getMessage(),e);
        }
       
        return result;
    }
    
    @GET
    @Path("/html/getServerList")
    @Produces(MediaType.TEXT_HTML)
    public String getServerListHTML() {
        String result="";
        result+="<ul>";
        try {
            for (Server s:m_service.getServer()) {
                if (ServerStatus.valueOf(s.getStatus())== ServerStatus.HEALTHY) {
                    result += "<li><a href=\"javascript:loadAMSServerControlPage('" + s.getIp() + "');\">" + s.getIp() + "</a></li>";
                } else {
                    result += "<li><a href=\"javascript:loadAMSServerControlPage('" + s.getIp() + "');\"><font color='red'>" + s.getIp()
                            + " is " +s.getStatus()+ "</font></a></li>";
                }
            }
        } catch (Exception e) {
            m_logger.error(e.getMessage(),e);
        }
        result+="</ul>";
        return result;
    }
    
    @GET
    @Path("/{ip}/start")
    @Produces(MediaType.TEXT_HTML)
    public String startServerHTML(@PathParam("ip") String ip) {
        String result="";
        try {
            for (String s:m_service.startServer(ip)){
                result+=s+"<br>";
            }
        } catch (Exception e) {
            result="<font color=\"#DC143C\">Error "+ip+" !</font>";
            m_logger.error(e.getMessage(),e);
        }
        return result;
    }
    
    @GET
    @Path("/{ip}/stop")
    @Produces(MediaType.TEXT_HTML)
    public String stopServerHTML(@PathParam("ip") String ip) {
        String result="";
        try {
            for (String s:m_service.stopServer(ip)){
                result+=s+"<br>";
            }
        } catch (Exception e) {
            result="<font color=\"#DC143C\">Error "+ip+" !</font>";
            m_logger.error(e.getMessage(),e);
        }
        return result;
    }
    
}
